package com.wd.tech.view.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.wd.tech.R;
import com.wd.tech.app.MyEditView;
import com.wd.tech.base.BaseActivity;
import com.wd.tech.base.BasePresenter;
import com.wd.tech.bean.FindCommunityListBean;
import com.wd.tech.bean.ReleasePostBean;
import com.wd.tech.fragment.SheQuFragment;
import com.wd.tech.mvp.IPresenterImpl;
import com.wd.tech.url.BaseUrl;
import com.wd.tech.utils.NetUtil;
import com.wd.tech.utils.RxPartMapUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;

public class ReleaseActivity extends BaseActivity {


    @BindView(R.id.send_cancel)
    TextView sendCancel;
    @BindView(R.id.send_doit)
    TextView sendDoit;
    @BindView(R.id.send_messedit)
    MyEditView sendMessedit;
    @BindView(R.id.iv)
    ImageView iv;
    private Uri uri=null;
    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        sendMessedit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.i("xx", "onTextChanged: " + charSequence.length());
                if (charSequence.length() <= 300) {
                    sendMessedit.setNowLength(charSequence.length() + "/300");
                } else {
                    Toast.makeText(ReleaseActivity.this, "最多输入300字", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        sendCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        sendDoit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = sendMessedit.getText().toString();
                if (TextUtils.isEmpty(s)){
                    Toast.makeText(ReleaseActivity.this, "内容为空", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    finish();
                }

                HashMap<String, RequestBody> map = new HashMap<>();
                map.put("content", RxPartMapUtils.toRequestBodyOfText(s));
                if (uri!=null&&uri.equals("")){
//                    File file = uri2File(uri);
//                    RequestBody requestBody = RxPartMapUtils.toRequestBodyOfImage(file);
//                    map.put("file\"; filename=\""+file.getName()+"", requestBody);

                    //mPresenter.IModelReleasePost(BaseUrl.ReleasePost,map,ReleasePostBean.class);
                }
            }
        });

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent img = new Intent(Intent.ACTION_PICK);
                img.setType("image/*");
                startActivityForResult(img,0);
            }
        });
    }

    @Override
    protected BasePresenter initPresenter() {
        return new IPresenterImpl();
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_release;
    }

    @Override
    public void success(Object o) {
        if (o instanceof ReleasePostBean && TextUtils.equals("0000", ((ReleasePostBean) o).getStatus())) {
            Toast.makeText(this, ((ReleasePostBean) o).getMessage(), Toast.LENGTH_SHORT).show();

        }
    }


    @Override
    public void Error(Throwable throwable) {

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==0){
            uri = data.getData();
            Glide.with(iv).load(uri).into(iv);
        }
    }
    private File uri2File(Uri uri) {
        String img_path;
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor actualimagecursor = managedQuery(uri, proj, null,
                null, null);
        if (actualimagecursor == null) {
            img_path = uri.getPath();
        } else {
            int actual_image_column_index = actualimagecursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToFirst();
            img_path = actualimagecursor
                    .getString(actual_image_column_index);
        }
        File file = new File(img_path);
        return file;
    }
}
